package com.xiaoxian.basic.exception;

import com.xiaoxian.basic.commoneumn.ErrorCode;
import com.xiaoxian.basic.util.AjaxResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public AjaxResult customExceptionExceptionHandler(CustomException e){
        e.printStackTrace();
        ErrorCode byCode = ErrorCode.getByCode(e.getMessage());
        return AjaxResult.my().setSuccess(false).setMsg(byCode.getMessage()).setCode(byCode.getCode());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        e.printStackTrace();
        //ErrorCode byCode = ErrorCode.getByCode(e.getMessage());
        return AjaxResult.my().setSuccess(false).setMsg("参数异常").setCode("400");
    }
    @ExceptionHandler(Exception.class)
    public AjaxResult exceptionHandler(Exception e){
        e.printStackTrace();
        ErrorCode byCode = ErrorCode.getByCode(e.getMessage());
        return AjaxResult.my().setSuccess(false).setMsg(byCode.getMessage()).setCode(byCode.getCode());
    }
}
